/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-08-02
 * Time: 14:26
 */
public class test2 {

    private static final int FINAL_NUMBER = 100;

    private static Object lock = new Object();

    private static int currentNum = 1;

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while (currentNum<FINAL_NUMBER){
                synchronized (lock){
                    while (currentNum%3==1){
                        System.out.println("t1:"+currentNum);
                        currentNum++;
                        lock.notifyAll();
                    }
                    try {
                        if(currentNum>=FINAL_NUMBER){
                            break;
                        }
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

        Thread t2 = new Thread(()->{
            while (currentNum<FINAL_NUMBER){
                synchronized (lock){
                    while (currentNum%3==2){
                        System.out.println("t2:"+currentNum);
                        currentNum++;
                        lock.notifyAll();
                    }
                    try {
                        if(currentNum>=FINAL_NUMBER){
                            break;
                        }
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

        Thread t3 = new Thread(()->{
            while (currentNum<FINAL_NUMBER){
                synchronized (lock){
                    while (currentNum%3==0){
                        System.out.println("t3:"+currentNum);
                        currentNum++;
                        lock.notifyAll();
                    }
                    try {
                        if(currentNum>=FINAL_NUMBER){
                            break;
                        }
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
    }
}
